﻿M.map.CityList = function (a) {
    T.lang.Class.call(this);
    this._key = "BAIDU_DATA";
    this.BUFF = {};
    this.P_MSG = "请选择";
    this.C_MSG = "请选择";
    this.B_MSG = "请选择";
    this.GETDATAERROR = "取得数据失败";
    this.REPLACELIST = {
        "黑龙江省": "黑龙江",
        "新疆维吾尔自治区": "新疆",
        "西藏自治区": "西藏",
        "广西壮族自治区": "广西",
        "宁夏回族自治区": "宁夏",
        "内蒙古自治区": "内蒙古",
        "香港特别行政区": "香港",
        "澳门特别行政区": "澳门",
        "大兴安岭地区": "大兴安岭",
        "临夏回族自治州": "临夏",
        "甘南藏族自治州": "甘南",
        "延边朝鲜族自治州": "延边",
        "海西蒙古族藏族自治州": "海西",
        "海北藏族自治州": "海北",
        "海南藏族自治州": "海南",
        "黄南藏族自治州": "黄南",
        "玉树藏族自治州": "玉树",
        "果洛藏族自治州": "果洛",
        "克孜勒苏柯尔克孜自治州": "克孜勒苏",
        "巴音郭楞蒙古自治州": "巴音郭楞",
        "博尔塔拉蒙古自治州": "博尔塔拉",
        "伊犁哈萨克自治州": "伊犁哈萨克",
        "昌吉回族自治州": "昌吉",
        "恩施土家族苗族自治州": "恩施",
        "保亭黎族苗族自治县": "保亭",
        "昌江黎族自治县": "昌江",
        "陵水黎族自治县": "陵水",
        "琼中黎族苗族自治县": "琼中",
        "乐东黎族自治县": "乐东",
        "白沙黎族自治县": "白沙",
        "黔南布依族苗族自治州": "黔南",
        "黔东南苗族侗族自治州": "黔东南",
        "黔西南布依族苗族自治州": "黔西南",
        "湘西土家族苗族自治州": "湘西",
        "楚雄彝族自治州": "楚雄",
        "红河哈尼族彝族自治州": "红河",
        "西双版纳傣族自治州": "西双版纳",
        "大理白族自治州": "大理",
        "怒江傈僳族自治州": "怒江",
        "迪庆藏族自治州": "迪庆",
        "德宏傣族景颇族自治州": "德宏",
        "文山壮族苗族自治州": "文山",
        "甘孜藏族自治州": "甘孜",
        "凉山彝族自治州": "凉山",
        "阿坝藏族羌族自治州": "阿坝",
        "彭水苗族土家族自治县": "彭水",
        "秀山土家族苗族自治县": "秀山",
        "酉阳土家族苗族自治县": "酉阳",
        "石柱土家族自治县": "石柱",
        "三都水族自治县": "三都",
        "玉屏侗族自治县": "玉屏",
        "松桃苗族自治县": "松桃",
        "沿河土家族自治县": "沿河",
        "印江土家族苗族自治县": "印江",
        "威宁彝族回族苗族自治县": "威宁",
        "道真仡佬族苗族自治县": "道真",
        "务川仡佬族苗族自治县": "务川",
        "紫云苗族布依族自治县": "紫云",
        "关岭布依族苗族自治县": "关岭",
        "镇宁布依族苗族自治县": "镇宁"
    };
    this._opts = a;
    this.url = "http://map.baidu.com/";
    this._paramerDeal();
    this.getSq = function (d, b, g) {
        var e = this;
        var c = M.map.CityList.sq;
        var f = "_fn_callback_" + c;
        M.map.CityList[f] = function (h) {
            if (g && typeof (g) == "function") {
                g.call(e, h, d, b)
            }
            setTimeout(function () {
                delete M.map.CityList[f]
            }, 3000)
        };
        M.map.CityList.sq++;
        return "M.map.CityList." + f
    };
    if (a.type == 2) {
        this._bindEvent();
        this._init2()
    } else {
        if (a.type == null || a.type == 1) {
            this._init1()
        }
    }
};
M.map.CityList.sq = 0;
M.map.CityList.prototype = {
    render: function (a) {
        var b = this;
        T.event.on(T.g(b.selectId), "click", function () {
            var c = b._dealData(b, a);
            if (c === false) {
                return false
            }
            T.g(b.containId).innerHTML = b.getHTML(c);
            if (b.isopen) {
                b.close();
                return
            } else {
                b.open()
            }
        })
    },
    close: function () {
        var a = this;
        baidu.event.un(document.body, "mousedown", a.clickHandler);
        T.g(a.boxId).style.display = "none";
        a.isopen = 0
    },
    open: function () {
        var a = this;
        T.g(a.boxId).style.display = "";
        T.event.on(document.body, "mousedown", a.clickHandler = function (c) {
            var b = c.srcElement || c.target;
            while (b) {
                if (b == T.g(a.boxId)) {
                    return
                }
                if (b == T.g(a.containId)) {
                    return
                }
                if (b == T.g("curCityText")) {
                    return
                }
                if (b == document.body) {
                    a.close();
                    return false
                }
                b = b.parentNode
            }
        });
        a.isopen = 1
    },
    _getCity: function (f) {
        var e = f.geo;
        var d = e.split("|")[1].replace(";", ",");
        var a = e.split("|")[2].replace(";", "");
        var c = this.REPLACELIST[f.area_name] != null ? this.REPLACELIST[f.area_name] : f.area_name;
        var b = {
            code: f.area_code,
            name: c,
            type: f.area_type,
            l: f.level,
            g: a,
            b: d
        };
        return b
    },
    _dealData: function (m, q) {
        if (q == null || q.result == null || q.result.error != 0) {
            if (!m._isEmpty(m.GETDATAERROR)) {
                alert(m.GETDATAERROR)
            }
            return false
        }
        var g = /市$/;
        var f = /特别行政区$/;
        var a = q.content.sub;
        var i = {};
        i.municipalities = {};
        i.provinces = [];
        i.other = {};
        var e = i.municipalities;
        var d = i.provinces;
        var k = i.other;
        for (var b in a) {
            var j = a[b];
            var o = this._getCity(j);
            if (g.test(j.area_name)) {
                e[j.area_code] = o
            } else {
                if (f.test(j.area_name)) {
                    k[j.area_code] = o
                } else {
                    d.push(o)
                }
            }
            if (j.sub == null) {
                return false
            }
            o.citys = [];
            for (var l in j.sub) {
                var n = j.sub[l];
                var h = this._getCity(n);
                o.citys.push(h)
            }
        }
        return i
    },
    _setData: function (a) {
        this._data[this._dataNum] = a;
        this._dataNum++
    },
    getHTML: function (f) {
        var e = this;
        if (e.tmphtml) {
            return e.tmphtml
        }
        T.event.on(T.g(e.closeBtId), "click", function () {
            e.close()
        });
        var b = [];
        b.push("<dl>");
        b.push("<dt>直辖市: </dt><dd>");
        for (var c in f.municipalities) {
            var d = f.municipalities[c];
            b.push('<a href="javascript:void(0)" onclick="baidu.lang.instance(\'' + this.guid + "').select(" + this._dataNum + ')">' + d.name + "</a>");
            e._setData(d)
        }
        b.push("</dd>");
        for (var c in f.provinces) {
            var d = f.provinces[c];
            b.push('<dt><a href="javascript:void(0)" class="black" onclick="baidu.lang.instance(\'' + this.guid + "').select(" + this._dataNum + ')">' + d.name + "</a>：</dt><dd>");
            e._setData(d);
            for (var a in d.citys) {
                b.push('<a href="javascript:void(0)" onclick="baidu.lang.instance(\'' + this.guid + "').select(" + this._dataNum + ')">' + d.citys[a].name + "</a>");
                e._setData(d.citys[a])
            }
            b.push("</dd>")
        }
        b.push("<dt>其他: </dt><dd>");
        for (var c in f.other) {
            var d = f.other[c];
            b.push('<a href="javascript:void(0)" onclick="baidu.lang.instance(\'' + this.guid + "').select(" + this._dataNum + ')">' + d.name + "</a>");
            e._setData(d)
        }
        b.push("</dd>");
        b.push("</dl>");
        e.tmphtml = b.join("");
        return e.tmphtml
    },
    select: function (e) {
        var d = this;
        var c = this._data[e];
        T.g(d.curCityId).innerHTML = c.name;
        d.close();
        var b = c.g.split(",");
        var a = d.map.getMapType().getProjection().pointToLngLat(new BMap.Pixel(b[0], b[1]));
        d.map.centerAndZoom(a, parseInt(c.l));
        if (d.selectCity) {
            d.selectCity(c)
        }
    }
};
M.map.CityList.prototype._init1 = function () {
    var b = this._opts;
    var a = {
        code: b.code,
        level: 2,
        callback: this.render
    };
    this._send(a)
};
M.map.CityList.prototype._init2 = function () {
    var b = this._opts;
    var c = b.def;
    if (this._isEmpty(c)) {
        return false
    }
    var a = {
        code: b.code,
        level: 1,
        type: c,
        callback: this._callback
    };
    this._send(a)
};
M.map.CityList.prototype._send = function (a) {
    if (a.code == 131 || a.code == 132 || a.code == 289 || a.code == 332) {
        var c = baidu.dom.g("borough");
        c.disabled = "disabled"
    } else {
        var c = baidu.dom.g("borough");
        c.disabled = ""
    }
    var b = this._getUrl(a);
    if (this.BUFF[b.key] != null) {
        a.callback.call(this, this.BUFF[b.key], a.type);
        return true
    }
    T.sio.callByBrowser(b.url)
};
M.map.CityList.prototype._getUrl = function (d) {
    var c = d.code;
    var g = d.level;
    var b = d.type;
    var f = d.callback;
    var a = null;
    if (this.url.indexOf("?") > -1) {
        a = this.url + "&qt=sub_area_list&ext=1&areacode=" + c + "&level=" + g
    } else {
        a = this.url + "?qt=sub_area_list&ext=1&areacode=" + c + "&level=" + g
    }
    var e = this.getSq(b, a, f);
    return {
        key: a,
        url: a + "&callback=" + e + "&t=" + (new Date()).getTime()
    }
};
M.map.CityList.prototype._callback = function (d, j, k) {
    var i = this;
    var a = this._opts;
    var e = a[j];
    var b = d;
    if (b == null || b.result == null || b.result.error != 0) {
        return false
    }
    if (e == null && e == "") {
        return false
    }
    sub = b.content.sub;
    switch (e) {
        case a.province:
            if (a.province != null && T.string.trim(a.province) != "") {
                T.g(a.province).options.length = 0
            }
        case a.city:
            if (a.city != null && T.string.trim(a.city) != "") {
                T.g(a.city).options.length = 0
            }
        case a.borough:
            if (a.borough != null && T.string.trim(a.borough) != "") {
                T.g(a.borough).options.length = 0
            }
            break
    }
    if (a.province == e) { }
    if (a.city == e) {
        T.g(e).options.add(new Option(this.C_MSG, ""))
    }
    if (a.borough == e) {
        T.g(e).options.add(new Option(this.B_MSG, ""))
    }
    if (T.g(e)[this._key] == null) {
        T.g(e)[this._key] = {}
    }
    if (e == "borough") {
        if (typeof (sub) == "undefined") {
            var g = baidu.dom.g("borough");
            g.disabled = "disabled"
        } else {
            var g = baidu.dom.g("borough");
            g.disabled = ""
        }
    }
    for (var c in sub) {
        var f = sub[c];
        f = this._getCity(f);
        T.g(e).options.add(new Option(f.name, f.code));
        T.g(e)[this._key][f.code] = f
    }
    var h = new BMap.LocalCity();
    h.get(function (m) {
        var l = new BMap.LocalSearch(a.map, {
            onSearchComplete: function (n) {
                var o = n.province;
                for (var p = 0; p < T.g(e).options.length; p++) {
                    if (T.g(e).options[p].text == o) {
                        if (o == "北京市") {
                            var q = baidu.dom.g("borough");
                            q.disabled = "disabled"
                        }
                        T.g(e).options[p].selected = true;
                        T.event.fire("province", "change")
                    }
                }
            }
        });
        l.search(m.name)
    });
    if (k != null && k != "") {
        this.BUFF[k] = b
    }
};
M.map.CityList.prototype._bindEvent = function () {
    var h = this;
    var a = h._opts;
    var d = a.province;
    var i = a.city;
    var j = a.borough;
    var c = a.eventType;
    var f = a.callbackProvince;
    var b = a.callbackCity;
    var g = a.callbackBorough;
    var e = function (m, l, n) {
        return m.pointToLngLat(new BMap.Pixel(l, n))
    };
    var k = function (q, m) {
        if (a.map != null) {
            var p = T.g(q)[h._key][m];
            var z = a.map.getMapType().getProjection();
            var r = p.g;
            var n = p.l;
            var v = r.split(",")[0];
            var t = r.split(",")[1];
            var w = e(z, v, t);
            var o = new BMap.Marker(w);
            o.enableDragging();
            var u = new BMap.Label("拖动改变位置", {
                offset: new BMap.Size("13", "-2")
            });
            o.setLabel(u);
            var s = new BMap.Icon("img/location.gif", new BMap.Size(14, 23));
            o.setIcon(s);
            map.clearOverlays();
            map.addOverlay(o);
            a.map.setCenter(w);
            a.map.setZoom(n)
        }
    };
    if (c != null && T.string.trim(c) == "") {
        c = "change"
    }
    if (d != null && T.string.trim(d) != "" && T.g(d)) {
        T.event.on(d, c, function (m) {
            var p = new BMap.LocalCity();
            p.get(function (r) {
                for (var q = 0; q < T.g("city").options.length; q++) {
                    if (T.g("city").options[q].text == r.name) {
                        T.g("city").options[q].selected = true
                    }
                    T.event.fire("city", "change")
                }
            });
            var o = T.event.getTarget(m);
            if (T.string.trim(o.value) == "") {
                return false
            }
            if (a.isAutoProvinceArea === true || a.map != null) {
                k(d, o.value)
            }
            var l = {
                code: o.value,
                level: "1",
                type: "city",
                callback: h._callback
            };
            h._send(l);
            if (f && typeof (f) == "function") {
                var n = T.g(d)[h._key][o.value];
                f(n)
            }
        })
    }
    if (i != null && T.string.trim(i) != "" && T.g(i)) {
        T.event.on(i, c, function (m) {
            var o = T.event.getTarget(m);
            if (T.string.trim(o.value) == "") {
                return false
            }
            if (a.isAutoCityArea === true || a.map != null) {
                k(i, o.value)
            }
            var l = {
                code: o.value,
                level: "1",
                type: "borough",
                callback: h._callback
            };
            h._send(l);
            if (b && typeof (b) == "function") {
                var n = T.g(i)[h._key][o.value];
                b(n)
            }
        })
    }
    if (j != null && T.string.trim(j) != "" && T.g(j)) {
        T.event.on(j, c, function (l) {
            var n = T.event.getTarget(l);
            if (T.string.trim(n.value) == "") {
                return false
            }
            if (a.isAutoBoroughArea === true || a.map != null) {
                k(j, n.value)
            }
            if (g && typeof (g) == "function") {
                var m = T.g(j)[h._key][n.value];
                g(m)
            }
        })
    }
};
M.map.CityList.prototype._paramerDeal = function () {
    var b = this._opts;
    var c = b.def;
    var a = b.province;
    var d = b.city;
    var e = b.borough;
    if (!this._isEmpty(b.url)) {
        this.url = b.url
    }
    if (this._isEmpty(c) && !this._isEmpty(a)) {
        c = "province"
    } else {
        if (this._isEmpty(c) && !this._isEmpty(d)) {
            c = "city"
        } else {
            if (this._isEmpty(c) && !this._isEmpty(e)) {
                c = "borough"
            }
        }
    }
    this._opts.def = c;
    this.map = b.map;
    this.tmphtml = 0;
    this.selectId = b.selectId;
    this.containId = b.containId;
    this.boxId = b.boxId;
    this.closeBtId = b.closeBtId;
    this.curCityId = b.curCityId;
    this.isopen = 0;
    this._data = {};
    this._dataNum = 0;
    this.selectCity = b.selectCity
};
M.map.CityList.prototype._isEmpty = function (a) {
    if (a == null || T.string.trim(a) == "") {
        return true
    } else {
        return false
    }
};